using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShootThemUp.Sources.GameElements.Weapons;
using Microsoft.Xna.Framework;
using ShootThemUp.Sources.GameElements;

namespace ShootThemUp.Sources.GameElements.Ships
{
    /// <summary>
    /// This abstract class represents a ship, redefined by player ship or ennemies ship.
    /// A ship can move and shoot.
    /// </summary>
    public abstract class Ship : MovableSprite
    {
        #region Fields

        // The initial life of this ship.
        private int initialLife;
        // The current life of this ship.
        private int life;
        //The current weapon of the ship.
        private Weapon weapon;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the initial life.
        /// </summary>
        /// <value>
        /// The initial life.
        /// </value>
        public int InitialLife
        {
            get { return initialLife; }
            set { initialLife = value; }
        }

        /// <summary>
        /// Gets or sets the life left.
        /// </summary>
        /// <value>
        /// The life left.
        /// </value>
        public int Life
        {
            get { return life; }
            set { life = value; }
        }

        /// <summary>
        /// Gets or sets the current weapon of the ship.
        /// </summary>
        /// <value>
        /// The current weapon of the ship.
        /// </value>
        public Weapon Weapon
        {
            get { return weapon; }
            set { weapon = value; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Ship"/> class.
        /// </summary>
        /// <param name="life">The initial life of the ship.</param>
        public Ship(int life, Weapon weapon)
        {
            this.initialLife = life;
            this.life = life;
            this.weapon = weapon;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Ship"/> class.
        /// </summary>
        /// <param name="position">The position of the sprite.</param>
        /// <param name="speed">The speed of the sprite.</param>
        /// <param name="sourceRectangle">The source rectangle of the sprite.</param>
        public Ship(int life, Weapon weapon, Vector2 position, Vector2 speed, Rectangle? sourceRectangle)
            : base(position, speed, sourceRectangle)
        {
            this.initialLife = life;
            this.life = life;
            this.weapon = weapon;
        }

        public Ship(int life, Weapon weapon, Vector2 position, Vector2 speed, Rectangle? sourceRectangle, Rectangle? hitbox) : base(position,speed,sourceRectangle,hitbox)
        {
            this.initialLife = life;
            this.life = life;
            this.weapon = weapon;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Updates the sprite position in function of the specified game time and the weapon data.
        /// </summary>
        /// <param name="gameTime">A snapshot of the current time.</param>
        public override void Update(GameTime gameTime)
        {
            if (weapon != null)
            {
                this.weapon.Update(gameTime);
                base.Update(gameTime);
            }
        }

        /// <summary>
        /// Determines whether this ship can shoot.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this ship can shoot; otherwise, <c>false</c>.
        /// </returns>
        public virtual bool CanShoot()
        {
            return this.weapon.CurrentShotTime <= 0;
        }

        /// <summary>
        /// Call this function when the shot is done.
        /// </summary>
        public virtual void ShotDone()
        {
            this.weapon.Reload();
        }

        #endregion
    }
}
